<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search2="search"
    />
    <a-card :bordered="false">
      <!-- <title-name title="项目设计月报列表" /> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :statistics-config="{ use: true, statistics, temp }"
        :exportConfig="{ use: true,select: '导出选中',all: '导出全部'}"
        :pageKeys="['pageNum', 'pageSize', 'total']"
      >
        <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
        <div slot="action" slot-scope="text, record">
          <MoreButton>
          <a @click="handleDels(record)">查看</a>
<!--          <span v-btnPermission="'ds_016_wri_02'" @click="handleEdit(record)">填报</span>-->
          <!-- 产品经理要求审批不通过也可以看到填报按钮 -->
          <a v-if="(record.fillStatus == 0 || record.fillStatus == 3)&&hideBuquBtn()" @click="handleEdit(record)">填报</a>
          <approval-button  v-if="record.fillStatus != 0" :id="record.id"></approval-button>

          <BasePrintButton v-if="record.fillStatus == 2" buttonType="a" :showDivider="false"
                           :beforePrintFn="() => beforePrintFn(record.id)"
          />
          </MoreButton>

        </div>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import {
  getDesignMonthlyReportTotal,
  getMonthlyReportList,
  print
} from '@/api/design/monthlyReport/monthlyReport'
import ApprovalDetails from '@/components/approvalDetails'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'

export default {
  name: 'a' + Date.now(),
  data() {
    return {
      beforePrintFn: async (id) => {
        let res = await print({ id })
        return res.data.content
      },
      columns: [
        { title: '序号', width: '65px', scopedSlots: { customRender: 'serial' } },
        { title: '项目名称', dataIndex: 'projectName' },
        { title: '分公司', dataIndex: 'branchComName' },
        { title: '月报时间', dataIndex: 'monthlyReportTime' },
        { title: '项目设计优化目标指标(万)', dataIndex: 'optimizeTarget' },
        { title: '填报时间', dataIndex: 'modifiedTime' },
        { title: '填报人', width: '100px', dataIndex: 'fillPerson' },
        { title: '状态', width: '100px', dataIndex: 'fillStatusName' },
        { title: '操作', dataIndex: 'action', width: 240, scopedSlots: { customRender: 'action' } }
      ],
      searchArr: [
        // { name: '项目', keyValue: 'branchComId', valueKey: 'projectId', type: SelectProject },
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // { name: '分公司', valueKey: 'branchComId', type: SelectSubCompany },
        { name: '月报时间阶段', value: 'createdTime', type: 'dateRange' },
        {
          name: '状态', value: 'fillStatus', type: 'select',
          data: [
            { value: '', label: '全部' },
            { value: '0', label: '待填报' },
            { value: '1', label: '审批中' },
            { value: '2', label: '审批通过' },
            { value: '3', label: '审批不通过' }
          ]
        }
      ],
      data: [],
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: async parameter => {
        if (this.queryParam.createdTime && this.queryParam.createdTime.length > 0) {
          this.queryParam.createStartTime = this.queryParam.createdTime[0].format('YYYY-MM-DD')
          this.queryParam.createEndTime = this.queryParam.createdTime[1].format('YYYY-MM-DD')
          delete this.queryParam.createdTime
        }
        this.getMonthlyReportsTotalReq()
        const requestParameters = Object.assign({}, parameter, {param:this.queryParam})
        let res = await getMonthlyReportList({}, requestParameters)
        if (res.code === this.SUCCESS_CODE) {
          return { code: 200, ...res.data, data: res.data.list }
        }
        return { code: 200, data: [] }
      },
      temp: () => {
        return (
          <span style="margin-right: 12px">
          待填写：<span class="totalNum">{this.statistics.draftNum || 0} 个</span> |
          审批中：<span class="totalNum">{this.statistics.submittedNum || 0} 个</span> |
          审批不通过：<span class="totalNum">{this.statistics.rejectedNum || 0} 个</span> |
          审批通过：<span class="totalNum">{this.statistics.pastNum || 0} 个</span>
        </span>
          // 审批通过的项目设计优化目标指标：<span class="totalNum">{this.statistics.pastOptimizeTarget || 0} 万</span>
        )
      },
      statistics: {},  // 统计
    }
  },
  created() {
  },
  computed: {},
  methods: {
    // 条件搜索
    search(val) {
      this.queryParam = val
      this.$refs.table.refresh(true)
    },
    // 列表统计
    async getMonthlyReportsTotalReq() {
      let res = await getDesignMonthlyReportTotal({}, this.queryParam)
      if (res.code === this.SUCCESS_CODE) {
        this.statistics = res.data
      }
    },
    // 详情
    handleDels(val) {
      this.$router.push({ path: '/project/design/monthlyReport/details', query: { id: val.id, type: 'view' } })
    },
    // 编辑
    handleEdit(val) {
      this.$router.push({ path: '/project/design/monthlyReport/fill', query: { id: val.id, type: 'edit' } })
    }
  }
}
</script>

